package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.model.ClassRoom;
import com.c611.smartclassroom.service.BuildService;
import com.c611.smartclassroom.service.ClassRoomService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/classroom")
public class ClassRoomController {

    @Autowired
    ClassRoomService classRoomService;

    @Autowired
    BuildService buildService;

    /**
     * 按分页查询，查询所属教学楼的所有教室
     * @param buildName
     * @param page
     * @param limit
     * @return
     */
    //localhost/classroom/queryAllClassRoom?buildID=2&page=1&pageSize=5
    @PostMapping("/queryAllClassRoom")
    public DataGridView<ClassRoom> queryAllClassRoom(@RequestParam("schoolID") String schoolID, @RequestParam("buildName") String buildName, @RequestParam("page") Integer page, @RequestParam("limit")Integer limit){

        if("".equals(schoolID) || "".equals(buildName))
            return new DataGridView<ClassRoom>(0,"ok",classRoomService.queryAllClassRoomCount(),classRoomService.queryAllClassRoom(page,limit));

        String buildID = buildService.queryBuildID(schoolID,buildName);
//        System.out.println("\n\n"+buildID+"\n\n");
        //查询页数是否合理
        if (page < 1 || limit < 1) return null;
        //查询数据库中的工单表中的工单总数
        long ClassRoomCount = classRoomService.queryAllClassRoomCountByBuildID(buildID);

        //计算该页是否为空（前一页的记录数都大于数据库总数）
        if ((page - 1) * limit >= ClassRoomCount) return null;
        //参数正确，执行分页查询操作

        return new DataGridView<ClassRoom>(0,"ok",ClassRoomCount,classRoomService.queryAllClassRoom(buildID,page,limit));
    }

    /**
     * 根据教学楼id查询该楼下的所有教室
     */
    @PostMapping("/queryClassRoomById")
    public DataGridView<ClassRoom> queryClassRoomById(@RequestParam("buildID")String buildID,@RequestParam("page") Integer page, @RequestParam("limit")Integer limit){

        if(buildID== null) return null;
        //查询页数是否合理
        if (page < 1 || limit < 1) return null;
        //查询数据库中的工单表中的工单总数
        long ClassRoomCount = classRoomService.queryAllClassRoomCountByBuildID(buildID);

        //计算该页是否为空（前一页的记录数都大于数据库总数）
        if ((page - 1) * limit >= ClassRoomCount) return null;
        //参数正确，执行分页查询操作

        return new DataGridView<ClassRoom>(0,"ok",ClassRoomCount,classRoomService.queryClassRoomById(buildID));
    }


    /**
     * 根据教学楼id查询该楼下的所有教室
     */
    @GetMapping("/queryClassRoomById")
    public List<ClassRoom> queryClassRoomById(String buildID){

        if(buildID== null) return null;

        return classRoomService.queryClassRoomById(buildID);
    }


    /**
     * 根据教室ID对教室信息进行编辑
     * @param classRoom
     * @return
     */
    //localhost/classroom/save?classroomid=1&tbuildid=2&classroom=xxx&delflag=0
    @PostMapping("/save")
    public int save(ClassRoom classRoom){

        if(classRoom== null) return 0;
        //获取前端传递的教室ID参数
        String classroomID = classRoom.getClassroomid();

        //获取前端传递的教室名称
        String classroomName = classRoom.getClassroom();

        //根据前端教室ID获取教室对象
        ClassRoom classRoom1 =  classRoomService.queryClassRoomByClassroomId(classRoom.getClassroomid());
//        System.out.println(classRoom1);
        //获取教学楼ID
        String buildID = classRoom1.getTbuildid();

        //组合成新的教室ID
        String classroomID2 = buildID + classroomName;

        //判断新的教室id是否在数据库中存在
//        if(classRoomService.queryClassRoomByClassroomId(classroomID2) != null) return 2;

        //判重,判断新的id是否在数据库中已经使用
        if(classRoomService.queryClassRoomByClassroomId(classroomID2) != null ) {
            return 2;
        }

        ClassRoom classRoom2 = new ClassRoom();
        classRoom2.setTbuildid(buildID);
        classRoom2.setClassroomid(classroomID2);
        classRoom2.setClassroom(classroomName);
        classRoom2.setAddtime(classRoom1.getAddtime());
        classRoomService.defClassRoom2(classroomID);

        /**
         * 进行连锁更改
         * 工单表的教室id,课表中的教室id，网关表中的教室id，设备表中的教室id,user表中授权教室的id
         */
        classRoomService.updateClassRoomIdOther(classroomID2,classroomID);

        //向数据库添加修改后的数据
        return classRoomService.addClassRoom(classRoom2);
    }

    /**
     * 向教室表中添加一条教室记录，教室信息classroomid，tbuildid，classroom，delflag不可为空
     * @param classRoom
     * @return
     */
    //localhost/classroom/addClassRoom?classroomid=7&tbuildid=121&classroom=xxx&delflag=0
    @PostMapping("/addClassRoom")
    public int addClassRoom(@Param("classroom")ClassRoom classRoom){

        if(classRoom== null) return 0;
        //检查教室ID是否已存在
        int flag = classRoomService.checkClassRoomId(classRoom.getClassroomid());
        if(flag == 1) return 2;
        classRoom.setAddtime(new Date());
        String classroomId = classRoom.getTbuildid() + classRoom.getClassroom();
        classRoom.setClassroomid(classroomId);
        return classRoomService.addClassRoom(classRoom);
    }

    /**
     * 根据教室ID删除教室（将置费标识赋值为1）
     * @param classroomID
     * @return
     */
    //localhost/classroom/defClassRoom?classroomID=7
    @PostMapping("/defClassRoom")
    public int defClassRoom(@RequestParam("classroomID") String classroomID){
        if(classroomID == null) return 0;
        return classRoomService.defClassRoom(classroomID);
    }
}
